package com.wanxin.car.carmng.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wanxin.car.carmng.dao.CarDaoImpl;
import com.wanxin.car.carmng.model.Car;

@Controller("/car")
public class CarAction {
	
	@Resource
	private CarDaoImpl dao;
	
//	@InitBinder  
//    protected void initBinder(WebDataBinder binder) {
//		System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
//        binder.registerCustomEditor(Date.class,new MyDateEditor(null, true));  
//    }
	
	/**
	 * 保存
	 */
	@RequestMapping(value="/car/save")
	public String save(@ModelAttribute Car car,@RequestParam(required=false) String aa){
		dao.save(car);
		return "redirect:/car/list";
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/car/delete")
	public String delete(@RequestParam() String id){
		dao.delete(id);
		return "redirect:/car/list";
	}
	
	/**
	 * 车辆列表
	 */
	@RequestMapping(value="/car/list")
	public String list(Model m){
		
		m.addAttribute("datas", dao.gets());
		
		return "car/list";
	}
	

	
	/**
	 * 新增 修改
	 */
	@RequestMapping(value="/car/input")
	public String input(@RequestParam(required=false) String id,Model m){
		
		if( StringUtils.hasText(id) ){
			//update
			m.addAttribute("model", dao.get(id));
		} else {
			//new
		}
		
		m.addAttribute("colors", Car.COLORS);//车辆颜色列表
		
		return "car/input";
		
	}

	public CarDaoImpl getDao() {
		return dao;
	}

	public void setDao(CarDaoImpl dao) {
		this.dao = dao;
	}
	
	
}
